"""
告警模型
"""
from sqlalchemy import Column, Integer, String, Enum, DateTime, Boolean, BigInteger
from sqlalchemy.sql import func
from app.models.base import Base
from typing import Optional

class Alarm(Base):
    """告警表模型"""
    __tablename__ = 't_alarms'
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='告警ID')
    time = Column(DateTime, nullable=False, comment='告警时间')
    level = Column(Enum('critical', 'warning', 'info'), nullable=False, comment='告警级别')
    type = Column(String(50), nullable=True, comment='告警类型(CPU/内存/磁盘/网络/应用/数据库)')
    source = Column(String(255), nullable=False, comment='告警来源(服务器名或服务名)')
    message = Column(String, nullable=False, comment='告警消息')
    status = Column(Enum('pending', 'resolved', 'ignored', 'active'), nullable=False, default='active', comment='告警状态')
    resolved_at = Column(DateTime, nullable=True, comment='解决时间')
    resolved_by = Column(Integer, nullable=True, comment='解决人ID')
    resolve_note = Column(String, nullable=True, comment='解决备注')
    created_at = Column(DateTime, nullable=False, default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, nullable=False, default=func.now(), onupdate=func.now(), comment='更新时间')
    
    def to_dict(self) -> dict:
        """转换为字典"""
        return {
            'id': self.id,
            'time': self.time.isoformat() if self.time else None,
            'level': self.level,
            'type': self.type,
            'source': self.source,
            'message': self.message,
            'status': self.status,
            'resolved_at': self.resolved_at.isoformat() if self.resolved_at else None,
            'resolved_by': self.resolved_by,
            'resolve_note': self.resolve_note,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }